﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

namespace Web.Helper
{
    public static class Crypto
    {
        private static string AddSalt(string value)
        {
            return String.Concat(value, "#@{5c~P%=jjN)_>pPsXzuBSfnAwL,;xwe7G(}_#E%");
        }

        public static string ComputeSHA1(string value)
        {
            if (value == null)
                throw new ArgumentNullException("ComputeSHA1");

            value = AddSalt(value);

            SHA1Managed sha1 = new SHA1Managed();
            return Crypto.ByteToHex(sha1.ComputeHash(Encoding.UTF8.GetBytes(value)));
        }

        public static string ComputeMD5(string value)
        {
            if (value == null)
                throw new ArgumentNullException("ComputeSHA1");

            MD5 md5 = MD5.Create();
            return Crypto.ByteToHex(md5.ComputeHash(Encoding.UTF8.GetBytes(value)));
        }

        public static string ByteToHex(byte[] value)
        {
            StringBuilder outValue = new StringBuilder();

            for (int i = 0; i < value.Length; i++)
                outValue.Append(value[i].ToString("X2"));

            return outValue.ToString();
        }
    }
}